using System;
using System.Web;
using SubSonic;
using SurveyHost.Core;
using SurveyHost.Core.DAL;
using Elmah;
using System.Reflection;
using System.Diagnostics;

namespace SurveyHost.Web
{

    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {
            // may throw, bringing down the website if missing answer types
            AddinHelper.RefreshAnswerObjectTypes();

            HostConfiguration hc = ConfigurationHelper.Configuration;
            if (hc.HasErrors())
                throw new InvalidOperationException("The configuration has errors.");

            //ErrorSignal.FromContext(HttpContext.Current).Raise(new ApplicationEventException("SurveyHost.NET version " + Assembly.GetExecutingAssembly().GetName().Version.ToString() + " has begun execution"));            
        }

        protected void Application_End(object sender, EventArgs e)
        {

        }

        ///// <summary>
        ///// Handles the Start event of the Session control.
        ///// </summary>
        ///// <param name="sender">The source of the event.</param>
        ///// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        //protected void Session_Start(object sender, EventArgs e)
        //{
        //    ErrorSignal.FromCurrentContext().Raise(
        //        new SessionEventException("Session created, ID: " + HttpContext.Current.Session.SessionID));
            
        //}
        ///// <summary>
        ///// Handles the End event of the Session control.
        ///// </summary>
        ///// <param name="sender">The source of the event.</param>
        ///// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        //protected void Session_End(object sender, EventArgs e)
        //{
        //    ErrorSignal.FromCurrentContext().Raise(
        //        new SessionEventException("Session destroyed, ID: " + HttpContext.Current.Session.SessionID));
        //}
    }

    [global::System.Serializable]
    public class SessionEventException : Exception
    {
        //
        // For guidelines regarding the creation of new exception types, see
        //    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconerrorraisinghandlingguidelines.asp
        // and
        //    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp07192001.asp
        //

        public SessionEventException() { }
        public SessionEventException(string message) : base(message) { }
        public SessionEventException(string message, Exception inner) : base(message, inner) { }
        protected SessionEventException(
          System.Runtime.Serialization.SerializationInfo info,
          System.Runtime.Serialization.StreamingContext context)
            : base(info, context) { }
    }

    [global::System.Serializable]
    public class ApplicationEventException : Exception
    {
        //
        // For guidelines regarding the creation of new exception types, see
        //    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconerrorraisinghandlingguidelines.asp
        // and
        //    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp07192001.asp
        //

        public ApplicationEventException() { }
        public ApplicationEventException(string message) : base(message) { }
        public ApplicationEventException(string message, Exception inner) : base(message, inner) { }
        protected ApplicationEventException(
          System.Runtime.Serialization.SerializationInfo info,
          System.Runtime.Serialization.StreamingContext context)
            : base(info, context) { }
    }
}